package com.deprice4u_client.products_on_sale;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;
import android.util.Pair;

import com.deprice4u_client.Deprice;

public class SocialSearcher {
	private String strBasicSearchURL = Deprice.SERVER_URL+"/searcher?action=extend";

	public Pair< ArrayList<SocialDataClass>,ArrayList<String> >  searchByKeyword(String strKeyword, int start_offset, int limit) {
		String strKeywordUtf8 = null;
		ArrayList<SocialDataClass> retSocialDataList = new ArrayList<SocialDataClass>();
		ArrayList<String> arryRelatedKeyword = new ArrayList<String>();
		
		try {
			URL queryURL=null; 
			
			if(strKeyword == null){
				queryURL = new URL(strBasicSearchURL+"&startt=" + start_offset +"&limit="+ limit);
			}else{
				try {
					strKeywordUtf8 = URLEncoder.encode(strKeyword, "UTF8");
				} catch (UnsupportedEncodingException e1) {
					Log.e(Deprice.TAG, e1.getMessage());
					return null;
				}
				queryURL = new URL(strBasicSearchURL+"&startt=" + start_offset +"&limit="+ limit + "&keyword=" + strKeywordUtf8);
			}
			
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(queryURL.openStream());

			if(strKeyword != null){
				NodeList relatedKeywordNode = doc.getElementsByTagName("related_keyword");
				String relatedKeyword = relatedKeywordNode.item(0).getChildNodes().item(0).getNodeValue();
				StringTokenizer st = new StringTokenizer(relatedKeyword, ",");

				arryRelatedKeyword = new ArrayList<String>();
				while(st.hasMoreElements()){
					arryRelatedKeyword.add(st.nextToken());
				}
			}
			
			NodeList items = doc.getElementsByTagName("item");

			int totalItems = items.getLength();
			Log.i(Deprice.TAG, "totalItems: " + totalItems);
			int i = 0;
			for (i = 0; i < totalItems; ++i) {
				Node item = items.item(i);
				if (item.getNodeType() == Node.ELEMENT_NODE) {
					SocialDataClass ItemData = new SocialDataClass();

					ItemData.title = ((Element) item)
							.getElementsByTagName("title").item(0)
							.getChildNodes().item(0).getNodeValue().trim();
					
					ItemData.link = ((Element) item)
							.getElementsByTagName("link").item(0)
							.getChildNodes().item(0).getNodeValue().trim();
					
					ItemData.social_provider = ((Element) item)
							.getElementsByTagName("social_provider").item(0)
							.getChildNodes().item(0).getNodeValue().trim();
					
				
					
					String s_price_now = ((Element) item)
							.getElementsByTagName("price_now").item(0)
							.getChildNodes().item(0).getNodeValue().trim();
					String s_price_original = ((Element) item)
							.getElementsByTagName("price_original").item(0)
							.getChildNodes().item(0).getNodeValue().trim();
					String s_area = ((Element) item)
							.getElementsByTagName("area").item(0)
							.getChildNodes().item(0).getNodeValue().trim();
					String s_photo_url = ((Element) item)
							.getElementsByTagName("photo_url").item(0)
							.getChildNodes().item(0).getNodeValue().trim();

					
					
					if (!s_price_now.equals("None"))
						ItemData.price_now = Integer.parseInt(s_price_now);
					else
						ItemData.price_now = 0;

					if (!s_price_now.equals("None"))
						ItemData.price_original = Integer
								.parseInt(s_price_original);
					else
						ItemData.price_original = 0;

					if (s_area.equals("None"))
						ItemData.area = "";
					else
						ItemData.area = s_area;

					if (s_photo_url.equals("None"))
						ItemData.photo_url = "";
					else
						ItemData.photo_url = s_photo_url;

					// ItemData.price_original = Integer.parseInt();

					Log.i(Deprice.TAG, "title: " + ItemData.title);
					Log.i(Deprice.TAG, "social_provider: "
							+ ItemData.social_provider);
					Log.i(Deprice.TAG, "photo url: " + ItemData.photo_url);

					/* TODO: Need to add code */

					retSocialDataList.add(ItemData);
				}
			}

		} catch (IOException ie) {
			Log.e(Deprice.TAG, ie.getMessage() + " IOException");
			return null;
		} catch (Exception e) {
			Log.e(Deprice.TAG, e.getMessage() + " Exception");
			return null;
		}

		return (new Pair< ArrayList<SocialDataClass>,ArrayList<String> >(retSocialDataList,arryRelatedKeyword));
	}

}
